<template>
  <div class="common_wrap">
    <div class="common_title">镇(街)指标管理</div>

    <el-form
      ref="queryForm"
      :model="queryParams"
      label-width="75px"
      inline
      class="search-nav"
    >
      <el-form-item prop="periodNo" label="导入期号">
        <el-select v-model="queryParams.periodNo" clearable>
          <el-option
            v-for="(item, index) in dateList"
            :key="index"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>

      <el-form-item prop="areaId" label="区域">
        <el-cascader
          v-model="queryParams.areaId"
          :options="areaList"
          filterable
          :props="{
            value: 'areaId',
            label: 'areaName',
            children: 'childList',
            checkStrictly: true,
          }"
        ></el-cascader>
      </el-form-item>

      <el-form-item prop="quotaName" label="指标名称">
        <el-input
          v-model="queryParams.quotaName"
          maxlength="20"
          placeholder="请输入指标名称"
          clearable
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >查询</el-button
        >
        <el-button
          type="primary"
          plain
          icon="el-icon-refresh"
          @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <div class="btn-container" style="text-align: right; margin-bottom: 15px">
      <!-- <el-button type="primary" icon="el-icon-plus" @click="toImport" disabled >指标数据录入</el-button> -->
      <el-button
        type="primary"
        icon="el-icon-warning-outline"
        @click="checkError"
        >最新异常数据</el-button
      >
      <el-button type="primary" icon="el-icon-download" @click="exportHandle"
        >下载模板</el-button
      >
      <el-button type="primary" icon="el-icon-upload2" @click="importHandle"
        >指标数据导入</el-button
      >
    </div>

    <el-table ref="table" v-loading="tableLoading" :data="tableData">
      <el-table-column
        prop="periodNo"
        label="导入期号"
        align="center"
        width="100"
      />
      <el-table-column
        prop="issueStartNo"
        label="开始期号"
        align="center"
        width="100"
      />
      <el-table-column
        prop="issueEndNo"
        label="结束期号"
        align="center"
        width="100"
      />
      <el-table-column prop="areaName" label="区域" align="center" />
      <el-table-column prop="streetName" label="镇(街)" align="center" />
      <el-table-column
        prop="empList"
        label="责任领导"
        align="center"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.empList.map((v) => v.empName).join("、") }}</span>
        </template></el-table-column
      >
      <el-table-column prop="quotaName" label="指标名称" align="center" />
      <el-table-column prop="quotaType" label="指标类型" align="center" />
      <el-table-column prop="position" label="位次" align="center" width="60" />
      <el-table-column prop="signage" label="赋牌" align="center" width="60">
        <template slot-scope="scope">
          <span :class="formateColor(scope.row.signage)">{{
            formateCard(scope.row.signage)
          }}</span>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="pagination-container"
      :background="true"
      :current-page="queryParams.page"
      :page-sizes="[10, 20, 30, 40, 50]"
      :page-size="queryParams.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="sizeChange"
      @current-change="currentChange"
    />

    <el-dialog
      :visible.sync="importVisible"
      title="指标数据导入"
      width="800px"
      :close-on-click-modal="false"
    >
      <el-form
        ref="importForm"
        :model="$data"
        label-width="80px"
        class="search-nav"
      >
        <el-form-item prop="issueNo" label="导入期号" class="is-required">
          <el-input
            v-model="importIssueNo"
            placeholder="请输入导入期号"
          ></el-input>
          最新期号：{{ this.dateList.at(-1) }}
        </el-form-item>
        <el-form-item prop="file" label="指标数据" class="is-required">
          <el-upload
            ref="upload"
            class="upload-demo"
            :headers="headers"
            :action="actionUrl"
            :limit="1"
            :on-success="successUp"
            :on-remove="deleteFile"
            :before-upload="beforeUpload"
            :data="uploadData"
            :auto-upload="false"
          >
            <el-button ref="files" size="small" type="primary"
              >选取文件</el-button
            >
            <div slot="tip" class="el-upload__tip">
              支持文件格式：.xls、.xlsx。
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeImport">取 消</el-button>
        <el-button type="primary" @click="confirmImport">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      :visible.sync="uploadVisible"
      title="导入指标信息"
      width="30%"
      :close-on-click-modal="false"
    >
      <div>{{ uploadMessage }}</div>
      <div
        slot="footer"
        class="dialog-footer"
        style="display: flex; justify-content: space-between"
      >
        <div>
          <el-button type="primary" @click="checkError" v-if="isError"
            >查看异常数据</el-button
          >
        </div>
        <div>
          <el-button @click="uploadVisible = false">取 消</el-button>
          <el-button type="primary" @click="uploadVisible = false"
            >确 定</el-button
          >
        </div>
      </div>
    </el-dialog>
  </div>
</template>



<script>
export default {
  data() {
    return {
      dateList: [],
      areaList: [],
      areaId: "",
      queryParams: {
        areaId: "",
        streetId: "",
        periodNo: "",
        quotaName: "",
        page: 1, // 第几页
        size: 10, // 每页条数
        periodNo: "",
      },

      total: 0, // 总条数
      tableLoading: false, // 表格loading
      tableData: [], // 表格数据

      importVisible: false,
      importIssueNo: "",
      importFile: "",

      headers: {
        Authorization: "Bearer " + localStorage.getItem("token"),
      },

      uploadMessage: "",
      isError: false,
      uploadVisible: false,
    };
  },
  computed: {
    actionUrl() {
      return process.env.VUE_APP_BASE_URL + "/webStreetQuota/import";
    },
    uploadData() {
      return { periodNo: this.importIssueNo };
    },
    formateColor() {
      return function (card) {
        const cardNum = card / 1;
        switch (cardNum) {
          case 0:
            return "green";
          case 1:
            return "red";
          default:
            return "blue";
        }
      };
    },
    formateCard() {
      return function (card) {
        const cardNum = card / 1;
        switch (cardNum) {
          case 0:
            return "绿";
          case 1:
            return "红";
          default:
            return "蓝";
        }
      };
    },
  },
  created() {
    this.getTableData();
    this.getAreaList();
    this.getDateList();
  },
  methods: {
    checkError() {
      this.$router.push({
        path: "/streetKeyError",
      });
    },
    getDateList() {
      this.$api.periodNoList().then((res) => {
        if (res.status == 200) {
          this.dateList = res.data.data;
        }
      });
    },
    closeImport() {
      this.importVisible = false;
      this.importIssueNo = "";
      this.importFile = "";
    },
    confirmImport() {
      console.log("upload");
      if (this.importIssueNo === "") {
        return this.$message.error("请输入期号");
      }
      this.$refs.upload.submit();
    },
    beforeUpload(file) {
      const fileName = file.name;
      const reg = /(.xls|.xlsx)$/;
      if (!reg.test(fileName)) {
        this.$message.error("请上传.xls、.xlsx格式的文件");
        return false;
      }
    },
    successUp(res, file) {
      if (res.code === 200) {
        this.$message.success("上传成功");
        this.uploadMessage = res.data;
        if (res.data.indexOf("失败") != -1) {
          this.isError = true;
        } else {
          this.isError = false;
        }
        this.uploadVisible = true;
      } else {
        this.$message.error(res.msg);
      }
      this.importVisible = false;
    },
    deleteFile() {
      this.importFile = "";
    },
    getAreaList() {
      this.$api
        .commonApi({
          _gp: "employee",
          _mt: "getAreaEmpTree",
        })
        .then((res) => {
          if (res.status == 200) {
            this.areaList = res.data.data.data;
            console.log(this.areaList);
          }
        });
    },
    getAreaList() {
      this.$api.getTreearea().then((res) => {
        if (res.status === 200) {
          this.areaList = this.getTreeData(res.data.data);
          console.log(this.areaList);
        }
      });
    },
    getTreeData(data) {
      for (var i = 0; i < data.length; i++) {
        if (data[i].childList.length < 1) {
          // children若为空数组，则将children设为undefined
          data[i].childList = undefined;
        } else {
          this.getTreeData(data[i].childList);
        }
      }
      return data;
    },
    exportHandle() {
      window.open(
        "http://47.99.223.66:3392/gbzj/10f6fd6c021e4b699f3081d084e7d121/乡镇指标模板.xlsx",
        "_blank"
      );
    },
    importHandle() {
      this.importVisible = true;
    },
    // 每页显示数量变化
    sizeChange(size) {
      this.queryParams.size = size;
      this.queryParams.page = 1;
      this.getTableData();
    },
    // 当前页变化
    currentChange(current) {
      this.queryParams.page = current;
      this.getTableData();
    },
    getTableData() {
      const params = { ...this.queryParams };
      console.log(params.areaId);
      if (Array.isArray(params.areaId)) {
        params.streetId = params.areaId[2] || "";
        params.areaId = params.areaId[1] || "";
      }
      this.$api.streetRecordPage(params).then((res) => {
        if (res.status == 200) {
          this.tableData = res.data.data.records;
          this.total = res.data.data.total;
        }
      });
    },
    handleQuery() {
      this.queryParams.page = 1;
      this.getTableData();
    },
    resetQuery() {
      this.queryParams = {
        areaId: "",
        periodNo: "",
        quotaName: "",
        page: 1,
        size: 10,
      };
      this.getTableData();
    },
  },
};
</script>



<style scoped>
.flex {
  display: flex;
}
.edit_box .date {
  font-size: 14px;
  color: #666;
  margin-bottom: 15px;
}
.edit_box .date > div:first-child {
  color: #333;
  width: 100px;
  text-align: right;
}
.edit_box .quota_info {
  font-size: 14px;
  color: #666;
  margin-bottom: 15px;
}
.edit_box .quota_info .left > div:first-child,
.edit_box .quota_info .right > div:first-child {
  color: #333;
  width: 100px;
  text-align: right;
}
.edit_box .quota_info .left > div:last-child,
.edit_box .quota_info .right > div:last-child {
  width: 100px;
}
.edit_box .inx_box .left {
  font-size: 14px;
  color: #666;
  margin-bottom: 15px;
}
.edit_box .inx_box .left .tit {
  height: 28px;
  line-height: 28px;
  color: #333;
  width: 100px;
  text-align: right;
}
.edit_box .inx_box .left .old {
  width: 60px;
  height: 26px;
  line-height: 26px;
  text-align: center;
  background-color: #efefef;
  border: 1px solid #dcdcdc;
  margin-right: 20px;
}
.edit_box .inx_box .change {
  height: 28px;
  line-height: 28px;
  color: #2689fd;
  margin-right: 20px;
}
.edit_box >>> .el-input__inner {
  padding-right: 0;
}
.edit_box .inx_box .right {
  margin-left: 20px;
}
.edit_box .inx_box .right .tit {
  height: 28px;
  line-height: 28px;
  color: #333;
  margin-right: 20px;
}
.edit_box .inx_box .right .card {
  width: 50px;
  height: 26px;
  line-height: 26px;
  text-align: center;
  color: #fff;
  background-color: #4ca5f1;
  margin-right: 20px;
}
.edit_box .inx_box .right .card.green {
  background-color: #5bc55a;
}
.edit_box .inx_box .right .card.red {
  background-color: #e43c26;
}
.edit_box .dgree_box .tit {
  color: #333;
  width: 100px;
  height: 28px;
  line-height: 28px;
  text-align: right;
}
.edit_box .dgree_box .content .tit {
  width: 60px;
  height: 28px;
  line-height: 28px;
  font-size: 14px;
  margin-bottom: 20px;
  color: #666;
}
.blue {
  color: #4ca5f1;
}
.green {
  color: #5bc55a;
}
.red {
  color: #e43c26;
}
</style>